package com.easpeed.comon.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.Gravity
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.blankj.utilcode.util.ScreenUtils
import com.easpeed.elevator.R
import com.easpeed.elevator.utils.NoticeDialog
import com.easpeed.comon.photo.PhotoUtils
import com.easpeed.comon.temp.UploadCacheData
import com.easpeed.comon.utils.CacheHelper
import com.easpeed.elevator.bean.LineBean
import com.easpeed.elevator.bean.RouteBean
import com.easpeed.elevator.databinding.DialogUploadCacheBinding


/**
 * Created by bigyu on 2024/11/12 13:37
 * com.easpeed.comon.dialog
 */
class UploadCacheDataDialog {
    var noticeDialog: NoticeDialog? = null

    private var mContext: Context? = null

    private var photoUtils: PhotoUtils? = null

    private var uploadCacheData: UploadCacheData? = null
//    private var cacheRouteList: ArrayList<RouteBean> = arrayListOf()

    var binding: DialogUploadCacheBinding? = null


    private var allFail: Boolean = true

    @SuppressLint("SetTextI18n")
    fun show(mContext: Context) {
        uploadCacheData = UploadCacheData(mContext)
        if (noticeDialog == null) {
            noticeDialog = NoticeDialog.Builder(mContext).setLayoutId(R.layout.dialog_upload_cache).setClickBtnDismissDialog(false).setCanCancel(true).setCanceledOnTouchOutside(true).create()
            noticeDialog?.setOnclickListener(R.id.close, View.OnClickListener { v: View? ->
                noticeDialog?.dismiss()
            })
            noticeDialog?.setDialogWith((ScreenUtils.getScreenWidth() * 0.8f).toInt(), (ScreenUtils.getScreenHeight() * 0.8f).toInt())
            noticeDialog?.setGravity(Gravity.CENTER)
            noticeDialog?.setCanceledTouchOutside(false)
        }

        binding = noticeDialog?.getView(R.id.root)?.let { DataBindingUtil.bind(it) }

        noticeDialog?.setOnclickListener(R.id.ok){
            if (!allFail) {
                CacheHelper.instance().save("isUploadCache", true)
            }
            noticeDialog?.dismiss()
        }
        noticeDialog?.setOnclickListener(R.id.cancel) {
            CacheHelper.instance().save("isUploadCache", true)
            noticeDialog?.dismiss()
        }

        var totalCount = 0
        var totalCountRoute = 0
        var list = ArrayList<LineBean>()
        var routeList = ArrayList<RouteBean>()
        uploadCacheData?.apply {
            list = getLineList()
            routeList = getRouteList()
            totalCount = getTotalTaskCount(list)
            totalCountRoute = getTotalRouteCount(routeList)
            if (totalCount > 0 || totalCountRoute > 0) {
                if (noticeDialog?.isShowing != true) {
                    noticeDialog?.show()
                }
            }
        }
        noticeDialog?.setOnclickListener(R.id.sure, View.OnClickListener { v: View? ->

            noticeDialog?.setVisibility(R.id.msg, View.GONE)
            noticeDialog?.setVisibility(R.id.layout_btn, View.GONE)
            val progressBar = noticeDialog?.getViewT<ProgressBar>(R.id.progress_bar)
            val progressText = noticeDialog?.getViewT<TextView>(R.id.progress)

            val progressRouteBar = noticeDialog?.getViewT<ProgressBar>(R.id.progress_bar_route)
            val progressRouteText = noticeDialog?.getViewT<TextView>(R.id.progress_route)


            uploadCacheData?.apply {

                if (totalCountRoute > 0) {
                    noticeDialog?.setVisibility(R.id.layout_route_progress, View.VISIBLE)
                }
                if (totalCount > 0) {
                    noticeDialog?.setVisibility(R.id.layout_progress, View.VISIBLE)
                }

                progressBar?.max = totalCount
                progressBar?.progress = 0
                progressRouteBar?.max = totalCountRoute
                progressRouteBar?.progress = 0

                var countSuccess = 0
                var countFail = 0

                var countSuccessRoute = 0
                var countFailRoute = 0

                this.setRouteProgressCallback {
                    if (it) {
                        countSuccessRoute++
                        allFail = false
                    } else {
                        countFailRoute++
                    }
                    progressRouteText?.text = "正在上传路由数据(${countSuccessRoute + countFailRoute}/${totalCountRoute})"
                    progressRouteBar?.progress = countSuccessRoute + countFailRoute
                    if (countSuccessRoute + countFailRoute == totalCountRoute) {
                        // 线路上传完成
                        noticeDialog?.setVisibility(R.id.ok, View.VISIBLE)
                    }
                }

                this.setLineProgressCallback {
                    if (it) {
                        countSuccess++
                        allFail = false
                    } else {
                        countFail++
                    }
                    progressText?.text = "正在上传线路数据(${countSuccess + countFail}/${totalCount})"
                    progressBar?.progress = countSuccess + countFail
                    if (countSuccess + countFail == totalCount) {
                        // 线路上传完成
                        if (totalCountRoute > 0) {
                            uploadRoute(routeList)
                        } else {
                            noticeDialog?.setVisibility(R.id.ok, View.VISIBLE)
                        }
                    }
                }
                if (totalCount > 0) {
                    uploadLine(list)
                } else {
                    if (totalCountRoute > 0) {
                        uploadRoute(routeList)
                    }
                }
            }
        })
    }

    private fun getTotalTaskCount(list: ArrayList<LineBean>): Int {
        var count = 0
        for (line in list) {
            val childCount = line.opticalList.count()
            count += childCount + 1
        }
        return count
    }

    private fun getTotalRouteCount(list: ArrayList<RouteBean>): Int {
        return list.count()
    }


}